<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-01-29 17:27:11
 * Desc:
 */

namespace app\api\controller;

use app\api\service\{GoodsService, WishRecommendService, ShareService};
use app\base\service\Pay\{WxPayService, OrderService};
use app\base\model\order\Order;
use app\base\model\address\{UserAddressInfo, UserPostInfo};
use app\base\model\user\{User, UserAccount, UserAccountLog};
use app\base\model\goods\{GoodsBaseInfo,
    GoodsGroupInfo,
    GoodsGroupJoinInfo,
    GoodsIntroduceInfo};
use app\base\model\system\SystemConfig;
use app\core\cache\RedisKeys;
use app\core\exception\{BusinessException, ErrorCode};
use app\core\util\PreVentRepeatSubmitUtil;
use app\core\log\Log4dd;
use think\Request;

class GoodsController extends ApiBaseController
{
    protected $beforeActionList = [
        'checkLogin',
        'checkAccountState',
        'getAccountType',
        'getSysCurrencyName'
    ];

    /**
     * Desc: 获取商品列表
     * Date: 2020/1/29 19:30
     * Author: yiyu
     * @return \think\response\Json
     */
    public function getGoodsList()
    {

        $page = input('page/d', 1);
        $size = input('page_size/d', 20);

        $goods_list = GoodsService::getGoodsList($this->user_id, $page, $size);

        $wish_coin_num = UserAccount::getWishCoinNumByUserId($this->user_id);
        return $this->successReturn(['goods_list' => $goods_list, 'wish_coin_num' => $wish_coin_num]);
    }

    /**
     * Desc: 好物详情
     * Date: 2020/2/1 22:08
     * Author: yiyu
     */
    public function getGoodsDetailInfo()
    {
        $goods_id = input('goods_id/d', 0);

        if (empty($goods_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品id不能为空');
        }

        $goods_info = GoodsBaseInfo::getGoodsBaseInfo($goods_id);

        if (empty($goods_info)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品不存在');
        }

        $goods_info['free_support_tips'] = SystemConfig::getSystemConfigByName('goods_detail_free_support_tips', '');

        $goods_carousel_img_list = GoodsIntroduceInfo::getGoodsImgList($goods_id, GoodsIntroduceInfo::INTRODUCE_TYPE_CAROUSEL);

        if (empty($goods_carousel_img_list)) {
            Log4dd::error('获取商品顶部轮播图失败');
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品不存在');
        }

        $goods_info['carousel_img_list'] = $goods_carousel_img_list;

        $goods_content_img_list = GoodsIntroduceInfo::getGoodsImgList($goods_id,
            GoodsIntroduceInfo::INTRODUCE_TYPE_CONTENT);

        if (empty($goods_content_img_list)) {
            Log4dd::error('获取商品详情轮播图失败');
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品不存在');
        }

        $goods_info['content_img_list'] = $goods_content_img_list;

        //获取我拥有的心愿币的数量
        $total_wish_coin = UserAccount::getWishCoinNumByUserId($this->user_id);

        //添加访问记录
        GoodsService::instance()->addVisitLog($this->user_id, $goods_id);

        $data = [
            'goods_info' => $goods_info,
            'my_wish_coin_num' => $total_wish_coin,
            'support_list' => WishRecommendService::getWishListByGoodsId($this->user_id, $goods_id, $total_wish_coin - $goods_info['need_wish_coin'])
        ];

        Log4dd::info('获取商品详情', $data);

        return $this->successReturn($data);

    }

    /**
     * Desc: 获取心愿详情
     * Date: 2020/2/1 22:09
     * Author: yiyu
     */
    public function getWishDetailInfo()
    {
        $group_id = input('group_id/d', 0);

        if (empty($group_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '心愿id不能为空');
        }

        $wish_info = GoodsService::instance()->getWishInfo($this->user_id, $group_id);

        if (empty($wish_info)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '心愿不存在');
        }

        $logistics_info = null;

        if ($wish_info['is_me'] && $wish_info['group_state'] == GoodsGroupInfo::GROUP_SUCCESS_STATE) {
            //获取物流信息
            $logistics_info = UserPostInfo::apiGetLogisticsInfo($group_id);
        }

        $button_info = GoodsService::instance()->getWishDetailInfoButtonText();

        $wish_info['button_info'] = $button_info;

        $join_info = GoodsGroupJoinInfo::getGroupJoinUserInfo($group_id);

        return $this->successReturn(['wish_info' => $wish_info, 'join_info' => $join_info, 'logistics_info' => $logistics_info]);

    }

    /**
     * Desc: 兑换模块，获取心愿值弹窗
     * Date: 2020/2/15 8:40
     * Author: yiyu
     */
    public function getExchangeModuleWishList()
    {
        $data = WishRecommendService::getExchangeModuleWishList($this->user_id);
        return $this->successReturn($data);

    }

    /**
     * Desc: 预发起心愿
     * Date: 2020/2/9 9:56
     * Author: yiyu
     */
    public function miniProgramPreInitiateWish()
    {
        $goods_id = input('goods_id/d', 0);

        if (empty($goods_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品id不能为空');
        }

        $goods_info = GoodsBaseInfo::getGoodsBaseInfo($goods_id);

        if (empty($goods_info)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品未找到');
        }

        $can_free = $goods_info['can_free'] ?? 0;

        $default_address = UserAddressInfo::getDefaultAddress($this->user_id);

        //获取我拥有的心愿币的数量
        $total_wish_coin = UserAccount::getWishCoinNumByUserId($this->user_id);

        $data = [
            'can_initiate' => $goods_info['need_wish_coin'] <= $total_wish_coin ? 1 : 0,
            'is_had_default_address' => $default_address ? 1 : 0,
            'goods_info' => $goods_info,
            'address_info' => $default_address,
            'my_wish_coin_num' => $total_wish_coin,
            'initiate_tips' => $can_free ? SystemConfig::getSystemConfigByName('free_support_tips', '') : SystemConfig::getSystemConfigByName('initiate_wish_confirm_txt', '发送心愿后，所有人将看到你的心愿，你可以邀请好友支持，加速达标~')
        ];

        return $this->successReturn($data);
    }

    /**
     * Desc: 小程序发起心愿
     * Date: 2020/1/30 15:19
     * Author: yiyu
     */
    public function miniProgramInitiateWish()
    {
        Log4dd::info('发起心愿', ['user_id' => $this->user_id, 'params' => Request::instance()->param(false)]);
        //防重复提交
        PreVentRepeatSubmitUtil::check(RedisKeys::getInitiateWishKey($this->user_id));

        $goods_id = input('goods_id/d', 0);

        if (empty($goods_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品id不能为空');
        }

        $goods_info = GoodsBaseInfo::getGoodsBaseInfo($goods_id);

        if (empty($goods_info)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品未找到');
        }

        $can_free = input('can_free/d');

        if ($can_free != $goods_info['can_free']) {
            return $this->successReturn(['not_can_free' => 1, 'not_can_free_tips' => '好物信息已更新，心愿发送失败，请重试~']);
        }

        $my_wish = input('my_wish/s', '', 'trim');

        empty($my_wish) && $my_wish = SystemConfig::getSystemConfigByName('default_wish', '');

        $address_id = input('address_id/d', 0);

        if (empty($address_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '地址id不能为空');
        }

        if (!UserAddressInfo::addressIsExists(['address_id' => $address_id, 'user_id' => $this->user_id])) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '地址信息不存在');
        }

        if ($goods_info['can_free'] == 1) {
            //当天只能发起指定次数免费心愿
            $today_can_initiate_free_wish_num = SystemConfig::getSystemConfigByName('today_can_initiate_free_wish_num', 0);
            //获取当天已发起的免费心愿个数
            $today_free_num = GoodsGroupInfo::getFreeWishNum($this->user_id);
            if ($today_free_num >= $today_can_initiate_free_wish_num) {
                if ($today_can_initiate_free_wish_num == 0) {
                    return $this->successReturn(['not_can_free' => 1, 'not_can_free_tips' => '好物信息已更新，心愿发送失败，请重试~']);
                } else {
                    return $this->successReturn(['not_can_free' => 2, 'not_can_free_tips' => '一天最多只可以发起' . $today_can_initiate_free_wish_num . '次#首次支持免费#心愿哦，你今天已经发起过' . $today_free_num . '次了，明天再来吧~']);
                }
            }
        }

        try {

            $wish_data = [
                'my_wish' => $my_wish,
            ];

            $group_info = GoodsService::instance()->initiateWish($this->user_id, $goods_id, $wish_data);

            $wish_coin_num = UserAccount::getWishCoinNumByUserId($this->user_id);
            if ($group_info && isset($group_info['group_id']) && $group_info['group_id']) {
                $wish_info = GoodsService::instance()->getWishInfo($this->user_id, $group_info['group_id']);
                //绑定收货信息
                GoodsService::instance()->bindAddress($this->user_id, $group_info['group_id'], $address_id);
                //$friend_need_pay_money = GoodsService::calculateNeedPayMoney($group_info);
                return $this->successReturn([
                    'group_id' => $group_info['group_id'],
                    'wish_coin_num' => $wish_coin_num,
                    'wish_info' => $wish_info,
                    'success_tips' => '心愿已发送至广场，所有人将能看到你的心愿',
                    'success_tips_110' => '好友支持￥' . $group_info['need_pay_money'] . '得'. config('heart_icon') . $goods_info['people_get_wish_coin'] . '，快去邀请好友支持！',
                    'support_txt' => '<span style="color: #666666;">还需<span style="color: #FF0000;">' . $group_info['need_people'] . '人</span>支持即可获得该心愿好物，<span style="color: #FF0000;">支持者得心愿值</span></span>',//'<span style="color: #666666;">还需<span style="color: #FF0000;">' . $group_info['need_people'] . '</span>人支持，好友支持￥' . $friend_need_pay_money . '得' . GoodsService::calculateCanGetWishCoinNum($friend_need_pay_money) . '心愿值</span>',
                    'share_button_txt' => '邀请好友支持',//'分享心愿给好友',
                    'need_people_num' => $group_info['need_people'],
                    'can_free_support' => $group_info['can_free_support'] ?? 0,
                    'initiate_wish_success_free_support_tips' => SystemConfig::getSystemConfigByName('initiate_wish_success_free_support_tips', '新用户首次支持免费，快去邀请好友支持！'),
                    'initiate_wish_success_bubble_tips' => SystemConfig::getSystemConfigByName('initiate_wish_success_bubble_tips', '<p><span style="color: #f8e71c;">首次</span>支持免费</p>'),
                ]);
            } else {
                return $this->errorReturn(ErrorCode::COMMON_ERROR, '发起心愿失败，请稍后再试');
            }

        } catch (BusinessException $e) {
            Log4dd::info("发起心愿失败:{$e->getMessage()}", func_get_args(), $e);
            return $this->errorReturn($e->getCode(), $e->getMessage());
        }


    }

    /**
     * Desc: 小程序取消心愿
     * Date: 2020/2/6 12:39
     * Author: yiyu
     * @return \think\response\Json
     */
    public function miniProgramCancelWish()
    {

        Log4dd::info('取消心愿', ['user_id' => $this->user_id, 'params' => Request::instance()->param(false)]);
        //防重复提交
        PreVentRepeatSubmitUtil::check(RedisKeys::getCancelWishKey($this->user_id));

        $group_id = input('group_id/d', 0);

        if (empty($group_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '要取消的心愿id为空');
        }

        try {
            GoodsService::cancelWish($this->user_id, $group_id);
            return $this->successReturn([], '取消成功，心愿值已返还到您的账户');
        } catch (BusinessException $e) {
            Log4dd::info("取消心愿失败:{$e->getMessage()}", func_get_args(), $e);
            return $this->errorReturn($e->getCode(), $e->getMessage());
        }

    }

    /**
     * Desc: 心愿值不足时获取我可以支持的心愿 或者是兑换列表获取心愿值获取我可以支持的心愿
     * Date: 2020/2/14 8:25
     * Author: yiyu
     * @return \think\response\Json
     */
    public function getCanSupportWishList()
    {

        Log4dd::info('获取我可以支持的心愿列表', ['user_id' => $this->user_id, 'params' => Request::instance()->param(false)]);

        $scene = input('scene/s', '');

        if (empty($scene)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '场景不能为空');
        }

        //物品详情的 product
        //兑换页面 exchange
        if (!in_array($scene, [WishRecommendService::GET_WISH_SCENE_PRODUCT, WishRecommendService::GET_WISH_SCENE_EXCHANGE])) {
            throw new BusinessException(ErrorCode::COMMON_INVALID_PARAMS, '未知场景');
        }

        switch ($scene) {
            case WishRecommendService::GET_WISH_SCENE_PRODUCT:
                $goods_id = input('goods_id/d', 0);
                if (empty($goods_id)) {
                    throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品id不能为空');
                }

                $goods_info = GoodsBaseInfo::getGoodsBaseInfo($goods_id);

                if (empty($goods_info)) {
                    throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品未找到');
                }

                //获取我拥有的心愿币的数量
                $total_wish_coin = UserAccount::getWishCoinNumByUserId($this->user_id);

                if ($total_wish_coin >= $goods_info['need_wish_coin']) {
                    throw new BusinessException(ErrorCode::COMMON_ERROR, '心愿值已足够兑换');
                }
                $wish_list = WishRecommendService::getWishListByWishCoinNum($this->user_id, $goods_info['need_wish_coin'] - $total_wish_coin );
                $support_wish_reward_info = GoodsService::SupportWishRewardInfo($this->user_id, $goods_info['people_get_wish_coin']);
                break;
            case WishRecommendService::GET_WISH_SCENE_EXCHANGE:
                $wish_list = WishRecommendService::getExchangeModuleWishList($this->user_id);
                $support_wish_reward_info = GoodsService::SupportWishRewardInfo($this->user_id);
                break;
            default:
                $wish_list = $support_wish_reward_info = [];
        }

        if ($wish_list) {
            $wish_list = array_slice($wish_list, 0, 30);
        }

        $data = [
            'wish_list' => $wish_list,
            'wish_coin_effect' => SystemConfig::getWishCoinEffect(),
            'support_wish_reward_info' => $support_wish_reward_info,//客户端需加上support_wish_reward_info.extra_give字段
        ];

        return $this->successReturn($data);

    }

    /**
     * Desc: 拉起支持弹窗信息
     * Date: 2020/2/1 20:02
     * Author: yiyu
     */
    public function getSupportWishBeforePopWindowsInfo()
    {
        $group_id = input('group_id/d', 0);

        if (empty($group_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '心愿id不能为空');
        }

        $group_info = GoodsGroupInfo::find($group_id);

        if (empty($group_info)) {
            throw new BusinessException(ErrorCode::DATA_NOT_FOUND, '未查询到心愿信息');
        }

        $goods_info = GoodsBaseInfo::getGoodsBaseInfo($group_info['goods_id']);

        if (empty($goods_info)) {
            throw new BusinessException(ErrorCode::DATA_NOT_FOUND, '未查询到商品信息');
        }

        if (empty($goods_info['people_get_wish_coin'])) {
            Log4dd::error('支持他人心愿支付后可获得的心愿币数量为0', [$group_id, $group_info['goods_id']]);
            throw new BusinessException(ErrorCode::COMMON_ERROR, '商品配置有误');
        }

        $support_wish_reward_info = GoodsService::SupportWishRewardInfo($this->user_id, $goods_info['people_get_wish_coin'], $group_info, $this->currency_name);

        //$can_initiate_goods_list = GoodsBaseInfo::getCanInitiateGoodsListBWishNum($can_get_info['wish_coin_num'], 3);

        $data = [
            'group_id' => $group_id,
            'goods_id' => $group_info['goods_id'],
            'can_support' => $group_info['group_state'] == 1 ? 1 : ($group_info['need_people'] >= $group_info['join_people'] ? 1 : 0),
            //是否可以继续支持
            'group_state' => $group_info['group_state'],
            //拼团状态 默认0,1进行中,2已完成,3已过期,4用户取消,5后台撤销
            'is_me' => $group_info['user_id'] == $this->user_id ? 1 : 0,
            'need_pay_money' => GoodsService::calculateNeedPayMoney($group_info),
            'wish_coin_effect' => SystemConfig::getWishCoinEffect(),
            'wish_coin_text' => $support_wish_reward_info['wish_coin_text'],
            'support_wish_reward_info' => $support_wish_reward_info,
            'is_first' => $support_wish_reward_info['is_first'] ?? 1,//是否首次 0 不是 1是,
            'wish_coin_num_text' => $support_wish_reward_info['i_can_get_text']
        ];

        Log4dd::info('获取支持前弹窗信息', ['user_id' => $this->user_id, 'group_id' => $group_id, 'data' => $data]);

        return $this->successReturn($data);

    }

    /**
     * Desc: 小程序支持TA人心愿
     * Date: 2020/2/1 20:02
     * Author: yiyu
     */
    public function miniProgramSupportWish()
    {
        Log4dd::info("小程序支持TA人心愿", Request::instance()->param(false));
        //防重复提交
        PreVentRepeatSubmitUtil::check(RedisKeys::getSupportWishKey($this->user_id));

        $app_id = input('app_id/s', '');

        if (empty($app_id)) {
            throw new BusinessException(ErrorCode::MINI_PROGRAM_LACK_PARAMS, 'app_id不能为空');
        }

        //支持一次支持多商品
        $group_ids = input('group_ids/s', '');

        $group_ids = array_unique(array_filter(explode(',', $group_ids)));

        if (empty($group_ids)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '要支持的group_id为空');
        }

        $params = [
            'pay_way' => Order::PAY_WAY_WX_MINI,
            'pay_mode' => Order::PAY_MODE_WX_PAY,
            'app_id' => $app_id
        ];

        if (count($group_ids) == 1) {
            $group_id = current($group_ids);
            $group_info = GoodsGroupInfo::find($group_id);

            if (empty($group_info)) {
                throw new BusinessException(ErrorCode::DATA_NOT_FOUND, '未查询到心愿信息');
            }

            if ($group_info['user_id'] == $this->user_id) {
                throw new BusinessException(ErrorCode::COMMON_ERROR, '不能支持自己的心愿');
            }

            if ($group_info['group_state'] == GoodsGroupInfo::GROUP_CANCEL_STATE) {
                throw new BusinessException(ErrorCode::COMMON_ERROR, '该心愿已经被发起者取消');
            }

            if ($group_info['group_state'] == GoodsGroupInfo::GROUP_SUCCESS_STATE) {
                throw new BusinessException(ErrorCode::COMMON_ERROR, '来晚了一步，该心愿的支持人数已经达标啦~');
            }

            if ($group_info['need_people'] > 0 && $group_info['join_people'] >= $group_info['need_people']) {
                throw new BusinessException(ErrorCode::COMMON_ERROR, '来晚了一步，该心愿的支持人数已经达标啦~');
            }

            //判断自己是否已支持
            $join_info = GoodsGroupJoinInfo::getGroupJoinUserInfoByUserId($group_id, $this->user_id);

            if ($join_info) {
                throw new BusinessException(ErrorCode::COMMON_ERROR, '您已经支持过一次啦~');
            }

            $had_support = GoodsGroupJoinInfo::hadSupport($this->user_id);

            Log4dd::info("小程序支持TA人心愿,判断是否可以免费支持", ['user_id' => $this->user_id, 'group_ids' => $group_ids, 'had_support' => $had_support, 'can_free_support' => $group_info['can_free_support']]);

            //首次免费支持,直接将订单置为成功，并更新
            if ($group_info['can_free_support'] == 1 && $had_support == false) {
                try {
                    //ip防刷
                    $ip = Request::instance()->ip();
                    $order_info = GoodsService::instance()->supportWish($this->user_id, $this->account_type, $group_ids, $params);
                    Log4dd::info("小程序支持TA人心愿,免费支持下单结果", ['user_id' => $this->user_id, 'group_ids' => $group_ids, 'order_info' => $order_info]);
                    OrderService::orderSuccess($this->user_id, 0, $order_info['out_trade_no'], true);//首次支持免费
                    return $this->successReturn(['is_free' => 1], '支持成功');//客户端is_free
                } catch (BusinessException $e) {
                    Log4dd::info("免费支持TA人失败:{$e->getMessage()}", func_get_args(), $e);
                    return $this->errorReturn($e->getCode(), '支持失败');
                }
            }

        }

        try {
            $order_info = GoodsService::instance()->supportWish($this->user_id, $this->account_type, $group_ids, $params);
            //这里客户端需要做两种处理，一种是需要去微信下单，一种是账号是机器人，直接返回支持成功
            if ($this->account_type == User::ACCOUNT_TYPE_ROBOT) {
                OrderService::orderSuccess($this->user_id, 0, $order_info['out_trade_no']);//直接说订单处理成功了
                return $this->errorReturn(ErrorCode::ROBOT_SUPPORT_SUCCESS, '支持成功');
            } else {
                $result = WxPayService::instance()->miniProgramCreateWxOrder($app_id, $order_info);
                Log4dd::info('微信下单成功，返回前端微信下单数据', $result);
                return $this->successReturn($result);
            }
        } catch (BusinessException $e) {
            Log4dd::info("支持TA人下单失败:{$e->getMessage()}", func_get_args(), $e);
            return $this->errorReturn($e->getCode(), '支持失败');
        }


    }

    /**
     * Desc: 支持成功后的弹窗相关信息
     * Date: 2020/2/1 21:33
     * Author: yiyu
     */
    public function getSupportWishSuccessPopWindowsInfo()
    {
        $group_id = input('group_id/d', 0);

        if (empty($group_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, 'group_id为空');
        }

        $group_info = GoodsGroupInfo::find($group_id);

        if (empty($group_info)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '心愿不存在');
        }

        $goods_info = GoodsBaseInfo::getGoodsBaseInfo($group_info['goods_id']);

        if (empty($goods_info)) {
            throw new BusinessException(ErrorCode::DATA_NOT_FOUND, '未查询到商品信息');
        }

        if (empty($goods_info['people_get_wish_coin'])) {
            Log4dd::error('支持他人心愿支付后可获得的心愿币数量为0', [$group_id, $group_info['goods_id']]);
            throw new BusinessException(ErrorCode::COMMON_ERROR, '商品配置有误');
        }

        return $this->successReturn([
            'share_info' => ShareService::miniProgramShareWish($group_id, $group_info['goods_id'], $this->user_id),
            'wish_coin_effect' => SystemConfig::getWishCoinEffect(),
            'support_wish_reward_info' => GoodsService::SupportWishRewardInfo($this->user_id, $goods_info['people_get_wish_coin'], $group_info, $this->currency_name)
        ]);

    }

    /**
     * Desc: 获取我发起的心愿列表
     * Date: 2020/2/2 23:13
     * Author: yiyu
     * @return \think\response\Json
     */
    public function getMyInitiateWishList()
    {
        $state = input('state/d', 0);

        $page = input('page/d', 1);
        $size = input('page_size/d', 10);
        /*if($state > GoodsGroupInfo::GROUP_SUCCESS_STATE){
            throw new BusinessException(ErrorCode::COMMON_INVALID_PARAMS, '参数错误');
        }*/

        $result = GoodsGroupInfo::getInitiateWishListByUserId($this->user_id, $state, $page, $size);

        return $this->successReturn($result);

    }

    /**
     * Desc: 获取我支持过的心愿列表
     * Date: 2020/2/2 23:20
     * Author: yiyu
     * @return \think\response\Json
     */
    public function getMySupportWishList()
    {
        $size = input('pageSize/d', 20);
        $page = input('page/d', 1);
        $result = GoodsGroupJoinInfo::getISupportListByUserId($this->user_id, $page, $size);
        return $this->successReturn($result);

    }

    /**
     * Desc: 分享心愿获取分享信息
     * Date: 2020/2/16 10:00
     * Author: yiyu
     * @return \think\response\Json
     */
    public function getWishShareInfo()
    {
        $group_id = input('group_id/d', 0);

        if (empty($group_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, 'group_id为空');
        }

        $group_info = GoodsGroupInfo::find($group_id);

        if (empty($group_info)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '心愿不存在');
        }

        return $this->successReturn(ShareService::miniProgramShareWish($group_id, $group_info['goods_id'], $this->user_id));
    }

    /**
     * Desc: 获取我的心愿值流水列表
     * Date: 2020/2/19 9:58
     * Author: yiyu
     * @return \think\response\Json
     */
    public function getMyBillList()
    {
        $page = input('page/d', 1);
        $size = input('page_size/d', 20);

        $data = UserAccountLog::getBillList(['user_id' => $this->user_id], ['create_time' => 'desc'], $page, $size);

        return $this->successReturn($data);
    }

    /**
     * Desc: 获取心愿详情福利弹窗
     * Date: 2020/3/5 23:16
     * Author: yiyu
     * @return \think\response\Json
     */
    public function getWishDetailWelfarePopUpsData()
    {
        $data = [];

        $group_id = input('group_id/d', 0);

        if (empty($group_id)) {
            return $this->successReturn($data);
        }

        $group_info = GoodsGroupInfo::where('group_id', $group_id)->field('user_id,can_free_support,group_state')->find();

        if (empty($group_info)) {
            return $this->successReturn($data);
        }

        if ($this->user_id == $group_info['user_id']) {
            return $this->successReturn($data);
        }

        $data = [];
        $give_info = UserAccountLog::hasAlreadyReceivedRegisterReward($this->user_id);

        if (empty($give_info)) {
            //判断用户是否付费
            $has_pay = Order::hasPay($this->user_id);
            if (!$has_pay) {
                $register_give_wish_coin_num = SystemConfig::getNewUserGiveWishCoinNum();

                $is_free = $group_info['can_free_support'] ?? 0;
                $welfare1 = [
                    'tips' => '福利一',
                    'title' => $register_give_wish_coin_num . $this->currency_name,
                    'content' => '可用于发起价值￥' . $register_give_wish_coin_num . '的好物心愿'
                ];

                $welfare2 = [
                    'tips' => '福利二',
                    'title' => '首次支持免费',
                    'content' => '支持当前好友心愿拥有免费特权'
                ];

                $welfare = [
                    'name' => 'welfare',
                    'background_color' => '',
                    'background_img' => '',
                    'data' => [
                        'top_title' => '',
                        'element' => [

                        ],
                        'button_info' => [
                            'button_name' => '立即领取',
                            'button_color' => '',
                        ]
                    ],
                ];

                if ($is_free == 1 && $group_info['group_state'] == GoodsGroupInfo::GROUP_RUN_STATE) {
                    $welfare['data']['top_title'] = '新人二重礼';
                    $welfare['data']['element'][] = $welfare1;
                    $welfare['data']['element'][] = $welfare2;
                } else {
                    $welfare['data']['top_title'] = '新人红包';
                    $welfare1['tips'] = '';
                    $welfare['data']['element'][] = $welfare1;
                }

                $data[] = $welfare;
            }
        }

        Log4dd::info('心愿详情福利弹窗', ['user_id' => $this->user_id, 'give_info' => $give_info, 'data' => $data]);
        return $this->successReturn($data);
    }

}